﻿using Newtonsoft.Json;
using PRO.Entity;
using PRO.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Common;
using PRO.Config;
using System.Data;
using PRO.Product;
using PRO.CashCoupon;

namespace web.deal
{
    public partial class submit_deal : PageBase
    {
        protected string Alert = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserInfo.ID > 0)
            {
                if (Session["GUID"] != null && !CVGRequest.GetFormString("_g.d").IsNullOrWhiteSpace())
                {
                    if (Session["GUID"].ToString().Equals(CVGRequest.GetFormString("_g.d")))
                    {
                        Session["GUID"] = null;
                        if (!IsPostBack)
                        {
                            int deliver = CVGRequest.GetFormString("_a.d").DEC_Decode(BaseConfigs.DEC_KEY).ToInt(0);
                            if (deliver == 0)
                            {
                                Alert = "alert('您没有选择送货地址！');history.go(-1);";
                                return;
                            }
                            if (!CVGRequest.GetFormString("_c.m").IsNullOrWhiteSpace())
                            {
                                CashCouponInfo cashcoupon = CashCouponLib.GetCashCoupon(CVGRequest.GetFormString("_c.m"));
                                if (cashcoupon != null)
                                {
                                    if (cashcoupon.Status != 1)
                                    {
                                        Alert = "alert('礼品卡无效，请重新输入！');history.go(-1);";
                                        return;
                                    }
                                }
                                else
                                {
                                    Alert = "alert('礼品卡无效，请重新输入！');history.go(-1);";
                                    return;
                                }
                            }
                            decimal TotalPrice = 0;
                            DataTable products = ShopCart.GetCartItems(out TotalPrice);
                            if (products != null && products.Rows.Count > 0)
                            {
                                Deliver_address_Info address = new Deliver_address_Info();
                                address = DeliverLib.GetDeliver_Address(deliver, UserInfo.ID);
                                OrderInfo info = new OrderInfo();
                                info.UserID = UserInfo.ID;
                                Random myRandom = new Random(Guid.NewGuid().GetHashCode());
                                info.OrderID = DateTime.Now.ToString("yyyyMMddhhmmssmmm") + UserInfo.ID + myRandom.Next(100, 999);
                                info.TotalPrice = TotalPrice;
                                info.CardNumber = CVGRequest.GetFormString("_c.h.p");
                                info.Consignee = address.Consignee;
                                info.Address = address.Address;
                                info.Zip = address.Zip;
                                info.Moble = address.Moble;
                                info.Phone = address.Phone;
                                for (int i = 0; i < products.Rows.Count; i++)
                                {
                                    info.Title += products.Rows[i]["title"].ToString() + " x " + products.Rows[i]["count"].ToString() + ";";
                                }
                                info.Des = "";
                                info.Note = CVGRequest.GetFormString("_note");
                                info.Status = 0;
                                info.BuyTime = DateTime.Now;
                                int newid = OrderLib.CreateOrder(info, products);
                                if (newid > 0)
                                {
                                    Utils.WriteCookie("ShopCart".DEC_Encode(BaseConfigs.DEC_KEY), string.Empty, 0);
                                    Response.Redirect("/deal/pay/" + newid.DEC_Encode(BaseConfigs.DEC_KEY));
                                }
                                else
                                    Alert = "alert('订单提交失败！');history.go(-1);";

                            }
                            else
                                Alert = "alert('您没有可购买的商品！');history.go(-1);";

                        }
                    }
                    else
                        Alert = "alert('参数不正确，请重新提交！');history.go(-1);";
                }
                else
                {
                    Alert = "alert('参数不正确，请重新提交！');history.go(-1);";

                }

            }
            else
                Response.Redirect("/login?url=" + Request.RawUrl.UrlEncode());
        }
    }
}